/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/4/22
 * Time: 22:42
 */

#include "Object.h"
void Object::Init()
{
}

void Object::Update(float _dt)
{
    for (auto child : object_to_add_)
        AddChild(child);

    object_to_add_.clear();

    auto itor = children_.begin();
    while(itor != children_.end()) {
        auto child = *itor;
        if(child->NeedRemove()) {
            itor = children_.erase(itor);
            child->Clean();
            delete child;
            child = nullptr;
        }
        else {
            if(child->IsActive())
                child->Update(_dt);
            ++itor;
        }
    }
}

void Object::Render()
{
    for(auto & child : children_) {
        if(child->IsActive())
            child->Render();
    }
}

void Object::Clean()
{
    for(auto & child : children_) {
        child->Clean();
    }
    children_.clear();
}

void Object::HandleEvent(SDL_Event & _event)
{
    for(auto & child : children_) {
        if(child->IsActive())
            child->HandleEvent(_event);
    }
}

void Object::AddChild(Object * _child)
{
    children_.emplace_back(_child);
}

void Object::RemoveChild(Object * _child)
{
    children_.erase(std::remove(children_.begin(), children_.end(), _child), children_.end());
}